"""
base文件
"""
import os
import ctypes
from dataclasses import dataclass


def load_lib(lib_name: str) -> ctypes.CDLL:
    """
    从path加载库，如果没有指定path就从默认位置寻找
    :param path:
    :param lib_name:
    :return:
    """
    for search_path in ["/usr/lib", "/usr/lib/x86_64-linux-gnu", "/usr/lib/arm-linux-gnueabihf"]:
        lib_path = os.path.join(search_path, lib_name)
        if os.path.isfile(lib_path):
            return ctypes.CDLL(lib_path, mode=ctypes.RTLD_GLOBAL)
    raise Exception(f"找不到{lib_name}")


@dataclass
class RobotLib:
    # dl: ctypes.CDLL = load_lib("libdl.so")
    # m: ctypes.CDLL = load_lib("libm.so")
    rt: ctypes.CDLL = load_lib("librt.so")

    modbus: ctypes.CDLL = load_lib("libmodbus.so")
    nlopt: ctypes.CDLL = load_lib("libnlopt.so")

    RobotIQLib: ctypes.CDLL = load_lib("libRobotIQLib.so")
    ExtraLib2: ctypes.CDLL = load_lib("libExtraLib2.so")
    ExtraLib: ctypes.CDLL = load_lib("libExtraLib.so")
    TorqueSensor: ctypes.CDLL = load_lib("libTorqueSensor.so")
    KinematicLibCustom: ctypes.CDLL = load_lib("libKinematicLibCustom.so")
    KinematicLib: ctypes.CDLL = load_lib("libKinematicLib.so")
    AdditionAxisLib: ctypes.CDLL = load_lib("libAdditionAxisLib.so")
    InterpolateLib: ctypes.CDLL = load_lib("libInterpolateLib.so")
    EtherCATLib: ctypes.CDLL = load_lib("libEtherCATLib.so")
    GripLib: ctypes.CDLL = load_lib("libGripLib.so")
    DynamicsLib: ctypes.CDLL = load_lib("libDynamicsLib.so")
    BaseLib: ctypes.CDLL = load_lib("libBaseLib.so")
    TorqueSensorLib: ctypes.CDLL = load_lib("libTorqueSensorLib.so")
    ExternallyGuidedMotion: ctypes.CDLL = load_lib("libExternallyGuidedMotion.so")
    RobotInterface: ctypes.CDLL = load_lib("libRobotInterface.so")
    RobotControlLib: ctypes.CDLL = load_lib("libRobotControlLib.so")
    RobotCalibrationLib: ctypes.CDLL = load_lib("libRobotCalibrationLib.so")
    RobotControlerAPI: ctypes.CDLL = load_lib("libRobotControlerAPI.so")
    RobotLib: ctypes.CDLL = load_lib("libRobotLib.so")
